package me.zhengjie.modules.finace_taxation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.finace_taxation.domain.FtEnterprise;
import me.zhengjie.modules.finace_taxation.service.FtEnterpriseService;
import me.zhengjie.modules.finace_taxation.service.dto.FtEnterpriseDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtEnterpriseQueryParam;
import me.zhengjie.modules.finace_taxation.service.mapper.FtEnterpriseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author 杨荣森
* @date 2021-06-29
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FtEnterpriseService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FtEnterpriseServiceImpl implements FtEnterpriseService {

    // private final RedisUtils redisUtils;
    private final FtEnterpriseMapper ftEnterpriseMapper;

    @Override
    public PageInfo<FtEnterpriseDto> queryAll(FtEnterpriseQueryParam query, Pageable pageable) {
        QueryWrapper<FtEnterprise> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FtEnterprise::getUserId, query.getUserId());
        IPage<FtEnterprise> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FtEnterprise> page = ftEnterpriseMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, FtEnterpriseDto.class);
    }

    @Override
    public List<FtEnterpriseDto> queryAll(FtEnterpriseQueryParam query){
        return ConvertUtil.convertList(ftEnterpriseMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), FtEnterpriseDto.class);
    }

    @Override
    public FtEnterprise getById(String id) {
        return ftEnterpriseMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public FtEnterpriseDto findById(String id) {
        return ConvertUtil.convert(getById(id), FtEnterpriseDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FtEnterpriseDto resources) {
        FtEnterprise entity = ConvertUtil.convert(resources, FtEnterprise.class);
        return ftEnterpriseMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(FtEnterpriseDto resources){
        FtEnterprise entity = ConvertUtil.convert(resources, FtEnterprise.class);
        int ret = ftEnterpriseMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return ftEnterpriseMapper.deleteBatchIds(ids);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<FtEnterpriseDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (FtEnterpriseDto ftEnterprise : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("企业名称", ftEnterprise.getName());
              map.put("注册码", ftEnterprise.getRegistrationNumber());
              map.put("隶属用户id", ftEnterprise.getUserId());
              map.put("企业联系人", ftEnterprise.getContactPerson());
              map.put("企业联系手机号", ftEnterprise.getContactPhone());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
